﻿
using System.Windows.Controls;
using NetBay.Core.BusinessEntities;
using Rio.Manager.Application.ViewModel.Administration;
using System.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using NetBay.Core.BusinessEntities.ActiveDirectory.Objects;
using GalaSoft.MvvmLight.Messaging;
using System.Collections.ObjectModel;
using Rio.Framework.Pages;
using Rio.Framework.Localisation;

namespace Rio.Manager.Application.Views
{
    public partial class GroupAssociationTabView : UserControl
    {
        #region Constructors

        public GroupAssociationTabView()
        {
            InitializeComponent();

            Unloaded += (s, e) => Messenger.Default.Unregister<NotificationMessage<ObservableCollection<Group>>>(this, RioGroupMappedAndAdInformation_SelectItems);

            Messenger.Default.Register<NotificationMessage<ObservableCollection<Group>>>(this, RioGroupMappedAndAdInformation_SelectItems);             
        }
               
        #endregion Constructors

        #region Event

        /// <summary>Test for selecting or not item</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadGridView_Oraganisation_RIO_Group_SelectionChanging(object sender, Telerik.Windows.Controls.SelectionChangingEventArgs e)
        {
            if ((DataContext as GroupAssociationTabViewModel).SelectedAdConfigCustomer == null)
            {
                //User error message.
                MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, Resx.GetLabel(ResxRioManagerGroup.GROUP_MSG_ASSOCIATION_AD_CONFIG_NOT_SELECTED), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                L_oMessage.Show();
                e.Cancel = true;
            }
        }

        /// <summary>
        /// Event when the user choose an RIO Group --> Fill Group and User mapped with Ad.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadGridView_Oraganisation_RIO_Group_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        { 
            //Setter RIO Group Organization selected.
            (DataContext as GroupAssociationTabViewModel).SelectedOrganizationRioGroup = new System.Collections.ObjectModel.ObservableCollection<Group>(RadGridView_Oraganisation_RIO_Group.SelectedItems.Cast<Group>());

            //Organization and Ad configuration choice, Save and Cancel button must to be enable or not.
            (DataContext as GroupAssociationTabViewModel).CanSaveModification = (DataContext as GroupAssociationTabViewModel).SelectedOrganizationRioGroup.Count != 0 ? true : false;
            (DataContext as GroupAssociationTabViewModel).CanCancelModification = (DataContext as GroupAssociationTabViewModel).SelectedOrganizationRioGroup.Count != 0 ? true : false;
            (DataContext as GroupAssociationTabViewModel).CanSelectOrganization = (DataContext as GroupAssociationTabViewModel).SelectedOrganizationRioGroup.Count != 0 ? false : true;
            (DataContext as GroupAssociationTabViewModel).CanSelectAdConfig = (DataContext as GroupAssociationTabViewModel).SelectedOrganizationRioGroup.Count != 0 ? false : true; 
        }

        /// <summary>
        /// Event when the user select Rio group mapped.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadGridView_Information_RIO_Group_Mapped_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            //Setter RIO Group mapped selected.
            (DataContext as GroupAssociationTabViewModel).SelectedAdObjectMapped = new System.Collections.ObjectModel.ObservableCollection<AdObject>(RadGridView_Information_RIO_Group_Mapped.SelectedItems.Cast<AdObject>());
        }

        /// <summary>Not selected item when there isn't rio group mapped selected.</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadGridView_Informations_Ad_SelectionChanging(object sender, Telerik.Windows.Controls.SelectionChangingEventArgs e)
        {
            if (((DataContext as GroupAssociationTabViewModel).SelectedOrganizationRioGroup == null) || ((DataContext as GroupAssociationTabViewModel).SelectedOrganizationRioGroup.Count == 0))
            {
                //User error message.                
                e.Cancel = true;
            }
        }

        /// <summary>
        /// Event when the user select Ad information to map.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadGridView_Informations_Ad_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            //Setter Ad information selected to map.
            (DataContext as GroupAssociationTabViewModel).SelectedAdObjectToMapped = new System.Collections.ObjectModel.ObservableCollection<AdObject>(RadGridView_Informations_Ad.SelectedItems.Cast<AdObject>());
        }

        /// <summary>Notification for not selection in the rio group grid.</summary>
        /// <param name="notification"></param>
        private void RioGroupMappedAndAdInformation_SelectItems(NotificationMessage<ObservableCollection<Group>> notification)
        {            
            //Clear Selection.
            RadGridView_Informations_Ad.SelectedItems.Clear();
            RadGridView_Oraganisation_RIO_Group.SelectedItems.Clear();            
        }

        #endregion Event 
    }
}